//package com.turing.post.capitalaccounts.impl;
//
//import java.math.BigInteger;
//import java.util.List;
//
//import javax.ejb.EJB;
//import javax.ejb.Local;
//import javax.ejb.Remote;
//import javax.ejb.Stateless;
//import javax.persistence.EntityManager;
//import javax.persistence.PersistenceContext;
//import javax.persistence.Query;
//
//import com.turing.post.capitalaccounts.util.BranchBankAccount;
//import com.turing.post.capitalaccounts.util.BranchBankAccountDao;
//import com.turing.post.capitalaccounts.util.CapitalApplicationException;
//import com.turing.post.capitalaccounts.util.CapitalRespCode;
//import com.turing.post.common.util.PCPSCommonDao;
//import com.turing.post.util.Config;
//import com.turing.post.util.GeneralDao;
//import com.turing.post.util.PCPSApplicationException;
//import com.turing.post.util.PCPSUtil;
//import com.turing.post.util.PCPSWebInput;
//
//@Stateless(mappedName = "capitalaccounts.impl.BranchBankAccountDaoImpl")
//@Remote
//@Local
//public class BranchBankAccountDaoImpl implements BranchBankAccountDao {
//	/**
//	 * 实体管理器
//	 */
//	@PersistenceContext
//	protected EntityManager _em;
//
//	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
//	private PCPSCommonDao pcpsCommonDaoImpl;
//	
//	@Override
//	public BranchBankAccount getBranchBankAccountByBranchBankAccountId(
//			BigInteger branchBankAccountId) {
//		return _em.find(BranchBankAccount.class, branchBankAccountId);
//	}
//	
//	@SuppressWarnings("unchecked")
//	@Override
//	public BranchBankAccount getBranchBankAccountBybankAccountId(
//			BigInteger bankAccountId) {
//		String sql = "select * from BranchBankAccount t where BankAccountId=?1";
//		Query query = _em.createNativeQuery(sql, BranchBankAccount.class);
//		query.setParameter(1, bankAccountId);
//		
//		List<BranchBankAccount> issueBankAccountList = query.getResultList();
//		for (BranchBankAccount vo : issueBankAccountList){
//			return vo;
//		}
//		return null;
//	}
//
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<BranchBankAccount> getBranchBankAccountList(
//			PCPSWebInput webInput,String branchNo, Integer page) {
//		if (webInput.getOperBranchNo() == null){
//			return null;
//		}
//		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
//		String sql = "select * from BranchBankAccount where IssueCompanyNo = ?1 and BranchNo = ?2 ";
//		sql += " order by BranchBankAccountId desc ";
//		int maxResult = Config.PAGE_SIZE;
//		int startPosition = maxResult * page;
//		Query query = _em.createNativeQuery(sql, BranchBankAccount.class)
//				.setFirstResult(startPosition).setMaxResults(maxResult);
//		query.setParameter(1, issueCompanyNo);
//		query.setParameter(2, branchNo);
//		return query.getResultList();
//	}
//
//	@Override
//	public BigInteger getBranchBankAccountTotal(PCPSWebInput webInput,String branchNo) {
//		if (webInput.getOperBranchNo() == null){
//			return null;
//		}
//		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
//		String sql = "select count(*) from BranchBankAccount where IssueCompanyNo = ?1 and BranchNo = ?2 ";
//		Query query = _em.createNativeQuery(sql);
//		query.setParameter(1, issueCompanyNo);
//		query.setParameter(2, branchNo);
//		return new BigInteger(query.getSingleResult().toString());
//	}
//
//	@Override
//	public void insertBranchBankAccount(PCPSWebInput webInput,
//			BranchBankAccount obj) throws PCPSApplicationException {
//		// 参数检查
//		if (obj == null || webInput == null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRMSG_NULLPOINTER,
//					CapitalRespCode.RC_ARGS_NULL, "参数为NULL");
//		}
//		// 平台机构管理员归属机构号为空
//		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					"", "超级管理员无法添加自己归属下的发卡方银行帐户信息!");
//		}
//		//机构号为发卡商号
//		if (webInput.getOperBranchNo().length() <= 6){
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					"", "发卡商无法添加自己为发卡商下辖机构银行帐户信息!");
//		}
//		// 判断BankAccountId银行帐户ID是否重复
//		if (getBranchBankAccountByBranchBankAccountId(obj.getBankAccountId()) != null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					"", "发卡商下辖机构银行帐户" + obj.getBankAccountId() + "已经存在!");
//		}
//		String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
//		obj.setIssueCompanyNo(issueCompanyNo);
//		obj	.setBranchNo(webInput.getOperBranchNo());
//		obj.setBranchBankAccountId(pcpsCommonDaoImpl.getUniqueIntegerKey());
//		GeneralDao.insert(_em, obj);
//	}
//
//	@Override
//	public void updateBranchBankAccount(PCPSWebInput webInput,BranchBankAccount obj)
//			throws PCPSApplicationException {
//		// 参数检查
//		if (obj == null || webInput == null) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRMSG_NULLPOINTER,
//					CapitalRespCode.RC_ARGS_NULL, "参数为NULL");
//		}
//		//检查修改的发卡商下辖机构银行帐户信息是否存在
//		BranchBankAccount branchBankAccount = getBranchBankAccountByBranchBankAccountId(obj.getBranchBankAccountId());
//		if (branchBankAccount == null){
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					"", "修改的发卡商下辖机构银行帐户信息不存在!");
//		}
//		//银行账户是不修改
//		if (branchBankAccount.getBankAccountId().compareTo(obj.getBankAccountId()) !=0 ){
//			//银行账户信息是不存在
//			if(getBranchBankAccountBybankAccountId(obj.getBankAccountId()) != null){
//				throw new CapitalApplicationException(
//						CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//						"", "发卡商下辖机构银行帐户" + obj.getBankAccountId()
//								+ "已经存在!");
//			}
//		}
//		branchBankAccount.setBankAccountId(obj.getBankAccountId());
//		branchBankAccount.setRemark(obj.getRemark());
//		GeneralDao.update(_em, branchBankAccount);
//	}
//
//	@Override
//	public void deleteBranchBankAccount(BranchBankAccount obj)
//			throws PCPSApplicationException {
//		String sql = "delete from BranchBankAccount where BranchBankAccountId=?1";
//		Query query = _em.createNativeQuery(sql);
//		query.setParameter(1, obj.getBranchBankAccountId());
//		try {
//			query.executeUpdate();
//		} catch (Exception e) {
//			throw new CapitalApplicationException(
//					CapitalApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					"", "删除发卡商下辖机构银行帐户银行帐户信息失败，该账户已被引用!");
//		}
//
//	}
//
//}
